app.controller("projectCtrl", function ($scope, $http, $window, userService, idService) {

    $scope.principle = {};
    $scope.isManager = false;
    $scope.isTester = false;
    $scope.isDeveloper = false;
    $scope.isRequire = false;

    idService.principle().then(function(value) {$scope.principle = value.data;});
    idService.isManager().then(function(value) {$scope.isManager = value.data;});
    idService.isTester().then(function(value) {$scope.isTester = value.data;});
    idService.isDeveloper().then(function(value) {$scope.isDeveloper = value.data;});
    idService.isRequire().then(function(value) {$scope.isRequire = value.data;});

    $scope.showPro = true;
    $scope.showDefect = false;

    $scope.pro = {
        "code": "",
        "name": "",
        "manager": "",
        "sn": "",
        "prefer": false
    };
    $scope.proList = [];
    $scope.devList = [];
    $scope.defectList = [];
    $scope.countPro = {};

    userService.devList().then(function (res) {
        $scope.devList = res.data.data;
    });

    $scope.getAll = function() {
        $http.get(
            "/project/getAll"
        ).then(function (value) {
            restResult(value, function (data) {
                $scope.proList = data;
            })
        })
    };

    $scope.add = function() {
        $http.post(
            "/project/add",
            $scope.pro
        ).then(function (res) {
            restResult(res);
            $window.location.reload();
        })
    };

    $scope.delete = function(code) {
        $http.post(
            "/project/delete",
            {
                "code": code
            }
        ).then(function (value) {
            restResult(value);
            $window.location.reload();
        })
    };

    $scope.setPrefer = function (code) {
        if (confirm("是否要将此项目设置为默认项目？")) {
            $http.post(
                "/project/prefer",
                {
                    "code": code
                }
            ).then(function (value) {
                restResult(value);
                $window.location.reload();
            })
        }
    };

    $scope.listDefect = function(pro) {
        $scope.showPro = false;
        $scope.showDefect = true;
        $scope.countPro = pro;
        $http.get("/his/list?proCode=" + pro.code + "&first=0&size=1000")
            .then(function (value) {
                restResult(value, function (data) {
                    $scope.defectList = data;
                })
            })
    };

    $scope.showProList = function () {
        $scope.showDefect = false;
        $scope.showPro = true;
        $scope.getAll();
    };

    $scope.hasResolved = 0;
    $scope.notResolved = 0;
    $scope.rank1 = 0;
    $scope.rank2 = 0;
    $scope.rank3 = 0;
    $scope.rank4 = 0;
    $scope.rank5 = 0;

    $scope.proCount = function (proCode) {
        $http.get("/count/rank1?proCode="+proCode).then(function (resp) { $scope.rank1 = resp.data });
        $http.get("/count/rank2?proCode="+proCode).then(function (resp) { $scope.rank2 = resp.data });
        $http.get("/count/rank3?proCode="+proCode).then(function (resp) { $scope.rank3 = resp.data });
        $http.get("/count/rank4?proCode="+proCode).then(function (resp) { $scope.rank4 = resp.data });
        $http.get("/count/rank5?proCode="+proCode).then(function (resp) { $scope.rank5 = resp.data });

        $http.get("/count/resolve?proCode="+proCode+"&resolved=true")
            .then(function (resp) { $scope.hasResolved = resp.data });
        $http.get("/count/resolve?proCode="+proCode+"&resolved=false")
            .then(function (resp) { $scope.notResolved = resp.data });
    }

});